<?php
class CartAction extends Action{
  public function _initialize(){//初始化
		R("Home.Public","top");
		R("Home.Public","bottom");
  }
  public function pre_detail(){//预处理
       /*安全处理*/
	checkget();
	checkpost();
	$pr_id = $_GET["pr_id"];
	if(empty($pr_id)||!is_numeric($pr_id))alert('参数错误');
	$buy_num = $_REQUEST['buy_num'];
	if(empty($buy_num)||!is_numeric($buy_num))alert('参数错误');

	/*安全处理*/
	
	$cart_pr = $_SESSION["cart_pr"];
	
	if(empty($cart_pr))$cart_pr = array();
	
	$pr_id_arr = array();
	foreach($cart_pr as $k=>$v){
		$pr_id_arr[] = $v['pr_id'];
	}
	
	
	if(false===($key=array_search($pr_id,$pr_id_arr))){
	   if($_GET["type"]=="combpr"){
		  $cart_pr[] = array("pr_id"=>$pr_id,"buy_num"=>$buy_num,'pr_market_price'=>$_GET['pr_market_price']);
	   }else{
		  $cart_pr[] = array("pr_id"=>$pr_id,"buy_num"=>$buy_num);  
	   }
	}else{
	   $cart_pr[$key]['buy_num'] = $cart_pr[$key]['buy_num']+$buy_num;
	}
	$_SESSION["cart_pr"] = $cart_pr;
	
	$this->redirect("index");
  }
  public function index(){
	$cart_pr = $_SESSION["cart_pr"];
	
	$product = M("product");
	$all_shop_price = $all_market_price = 0;
	foreach($cart_pr as $k=>$v){
		$pr_id = $v['pr_id'];
		$buy_num = $v['buy_num'];
		
		//套餐
		if(isset($v['pr_market_price'])){
			$combpr = M("combpr");
			$pr_info = $combpr->where("id = $pr_id")->find();
		    $pr_info["pr_name"]=$pr_info["comb_name"];
			$pr_info['pr_shop_price'] = $pr_info["total_price"];
			$pr_info["pr_market_price"] = $v['pr_market_price'];
			$comb_pr_ids=$pr_info["comb_pr_ids"];
			$first_pr_id =current(explode(",",$comb_pr_ids));
			$pr_info['pr_list_pic']=$product->where("id=$first_pr_id")->getField("pr_list_pic");
			$pr_info['pr_url']=U('Promotion/index');
			$pr_info['pr_type']="combpr";
			$pr_info["pr_sn"]="";//窗纱型号
			
		}else{
			$pr_info = $product->where("id = $pr_id")->find();
			$pr_info['pr_url']=U("Product/pr_info/id/".$pr_info['id']);
		}
		
		$cart_pr[$k]['pr_info'] = $pr_info;
		$cart_pr[$k]['sub_shop'] = $pr_info['pr_shop_price']*$buy_num; //售价总额
		$all_shop_price+=$pr_info['pr_shop_price']*$buy_num;
		$cart_pr[$k]['sub_market'] = $pr_info['pr_market_price']*$buy_num; //市场价总额 
		$all_market_price+=$pr_info['pr_market_price']*$buy_num;
	}
	//节省的金额
	$save_fee = $all_market_price - $all_shop_price;
	
	$this->assign("all_shop_price",$all_shop_price);
	$this->assign("all_market_price",$all_market_price);
	
	$this->assign("cart_pr",$cart_pr);
	$this->assign("save_fee",$save_fee);
	
	$_SESSION["cart_pr"] = $cart_pr;
	
	//计算总金额
	$this->display(); 
  }
  public function delete(){//删除购物车中的商品
     if(empty($_SESSION["cart_pr"]))alert('购物车已经为空!');
	 $id = $_GET["id"];
	 if(empty($id)||!is_numeric($id))alert("参数错误");
	 
	 $cart_pr = $_SESSION["cart_pr"];
	 $pr_id_arr = array();
	 foreach($cart_pr as $k=>$v){
		$pr_id_arr[] = $v['pr_id']; 
	 }
	 if(false!==($key = array_search($id,$pr_id_arr))){
		 unset($cart_pr[$key]);
		 $_SESSION["cart_pr"] = $cart_pr;
	 }else{
		 alert('购物车中没有该商品，无法删除'); 
	 }
	 $this->redirect("index");
  }
  public function deleteall(){//清空购物车
     if(empty($_SESSION["cart_pr"]))alert('购物车已经为空，不需要清空');
	 unset($_SESSION["cart_pr"]);
	 $this->redirect("index");
  }
  
  public function update_cart(){
	  /*安全处理*/
	  checkpost();
	  /*安全处理*/
	  
	  if(empty($_SESSION["cart_pr"]))alert('购物车为空,请先选择你要购买的商品',U("Product/index"));
	  
	  $buy_num_arr = $_POST["buy_num"];
	  
	  $pr_id_arr = $_POST["pr_id"];
	  
	  $cart_pr = $_SESSION["cart_pr"];
	  
	  foreach($cart_pr as $k=>$v){
		 $pr_id_arr[] = $v['pr_id'];  
	  }
	  foreach($pr_id_arr as $k=>$v){
		 if(false!==($key = array_search($v,$pr_id_arr))){
			$cart_pr[$key]['buy_num'] = $buy_num_arr[$key]; 
		 }
	  }
	  $_SESSION["cart_pr"] = $cart_pr;
	  $this->redirect("index");
  }
	
}
?>